
** set path
// gl path ""

** open log
cap log close
log using "$path/table1.log", replace

***** Descriptive statistics for county level analysis, presidential elections

use "$path/foreclosures_analysis.dta" if pres_vs != ., clear

sum foreclosure_rate
local m1 = r(mean)
local sd1 = r(sd)
local min1 = r(min)
local max1 = r(max)
local n1 = r(N)

sum foreclosure_rate [aw=pop_y]
local mw1 = r(mean)

// compute county-demeaned standard deviation
egen mean_foreclosure_rate = mean(foreclosure_rate), by(fips)
gen foreclosure_rate_demeaned = foreclosure_rate - mean_foreclosure_rate
sum foreclosure_rate_demeaned
local dsd1 = r(sd)

sum pres_vs
local m2 = r(mean)
local sd2 = r(sd)
local min2 = r(min)
local max2 = r(max)
local n2 = r(N)

sum pres_vs [aw=pop_y]
local mw2 = r(mean)

egen mean_pres_vs = mean(pres_vs), by(fips)
gen pres_vs_demeaned = pres_vs - mean_pres_vs
sum pres_vs_demeaned
local dsd2 = r(sd)


sum pop_y
local m3 = r(mean)
local sd3 = r(sd)
local min3 = r(min)
local max3 = r(max)
local n3 = r(N)

// Descriptive statistics for county level analysis, legislative elections

use "$path/foreclosures_leg_analysis.dta" if cong_vs != . | sen_vs != ., clear

sum foreclosure_rate if cong_vs != .
local m4 = r(mean)
local sd4 = r(sd)
local min4 = r(min)
local max4 = r(max)
local n4 = r(N)

sum foreclosure_rate [aw=pop_y] if cong_vs != .
local mw4 = r(mean)

egen mean_foreclosure_rate = mean(foreclosure_rate), by(fips)
gen foreclosure_rate_demeaned = foreclosure_rate - mean_foreclosure_rate
sum foreclosure_rate_demeaned
local dsd4 = r(sd)

sum sen_vs 
local m5 = r(mean)
local sd5 = r(sd)
local min5 = r(min)
local max5 = r(max)
local n5 = r(N)

sum sen_vs [aw=pop_y]
local mw5 = r(mean)

egen mean_sen_vs = mean(sen_vs), by(fips)
gen sen_vs_demeaned = sen_vs - mean_sen_vs
sum sen_vs_demeaned
local dsd5 = r(sd)

sum cong_vs
local m6 = r(mean)
local sd6 = r(sd)
local min6 = r(min)
local max6 = r(max)
local n6 = r(N)

sum cong_vs [aw=pop_y]
local mw6 = r(mean)

egen mean_cong_vs = mean(cong_vs), by(fips)
gen cong_vs_demeaned = cong_vs - mean_cong_vs
sum cong_vs_demeaned
local dsd6 = r(sd)

sum pop_y if cong_vs != .
local m7 = r(mean)
local sd7 = r(sd)
local min7 = r(min)
local max7 = r(max)
local n7 = r(N)

log close


quietly {
	cap log close
	set linesize 255
	log using "$path/descriptive_county.tex", text replace
	
	noisily dis "\begin{table}[t]"
	noisily dis "\centering"
	noisily dis "\small"
	noisily dis "\caption{\textbf{Descriptive Statistics, County Level, 2004--2016.}"
	noisily dis "\label{tab:descriptive_county}}"
	noisily dis "\resizebox{\textwidth}{!} {"
	noisily dis "\begin{tabular}{lccccccc}"
	noisily dis "\toprule \toprule"
	
	noisily dis " & & Pop-Weighted & Standard & County & & \\"
	noisily dis " & Mean & Mean & Deviation & Demeaned SD & Min & Max & N \\"
	noisily dis " & (1) & (2) & (3) & (4) & (5) & (6) & (7) \\[0mm]"
	noisily dis "\midrule"
	noisily dis " & \multicolumn{7}{c}{A. Presidential Elections} \\\cline{2-8}\\[-2mm]"
	noisily dis " Foreclosure Rate & " %4.2f `m1' " & " %4.2f `mw1' " & " %4.2f `sd1' " & " %4.2f `dsd1' " & " %4.2f `min1' " & " %4.2f `max1' " & " %8.0fc `n1' " \\"
	noisily dis " Dem Vote Pct & " %4.2f `m2' " & " %4.2f `mw2' " & " %4.2f `sd2' " & " %4.2f `dsd2' " & " %4.2f `min2' " & " %4.2f `max2' " & " %8.0fc `n2' " \\"
	noisily dis " County Population & " %8.0fc `m3' " & -- & " %8.0fc `sd3' " & -- & " %8.0fc `min3' " & " %12.0fc `max3' " & " %8.0fc `n3' " \\[1mm]\cline{2-8}\\[-4mm]"

	noisily dis " & \multicolumn{7}{c}{B. Legislative Elections} \\\cline{2-8}\\[-2mm]"
	noisily dis " Foreclosure Rate & " %4.2f `m4' " & " %4.2f `mw4' " & " %4.2f `sd4' " & " %4.2f `dsd4' " & " %4.2f `min4' " & " %4.2f `max4' " & " %8.0fc `n4' " \\"
	noisily dis " Dem Senate Vote Pct & " %4.2f `m5' " & " %4.2f `mw5' " & " %4.2f `sd5' " & " %4.2f `dsd5' " & " %4.2f `min5' " & " %4.2f `max5' " & " %8.0fc `n5' " \\"
	noisily dis " Dem House Vote Pct & " %4.2f `m6' " & " %4.2f `mw6' " & " %4.2f `sd6' " & " %4.2f `dsd6' " & " %4.2f `min6' " & " %4.2f `max6' " & " %8.0fc `n6' " \\"
	noisily dis " County Population & " %8.0fc `m7' " & -- & " %8.0fc `sd7' " & -- & " %8.0fc `min7' " & " %12.0fc `max7' " & " %8.0fc `n7' " \\"

	noisily dis "\bottomrule \bottomrule"
	noisily dis "\multicolumn{8}{p{1.05\textwidth}}{\footnotesize \emph{Note: } Panel A presents descriptive statistics "
	noisily dis "for the merged dataset on presidential elections.  In this panel, every observation is a county within "
	noisily dis "a four-year presidential election period.  Panel B presents descriptive statistics for the merged dataset "
	noisily dis "on legislative elections.  In this panel, every observation is a county within a two-year congressional "
	noisily dis "election period.  The foreclosure rate is calculated as the mean of annual total foreclosures divided by "
	noisily dis "county population in thousands of people, where the mean is computed over the relevant time period (either the four-year "
	noisily dis "presidential term of the two-year congressional term.)  Vote percentages are the Democratic percentage "
	noisily dis "of the two-party vote in a given county.  County population is linearly interpolated for non-Census years.  "
	noisily dis "The second column provides means that are weighted by county population.}"
	noisily dis "\end{tabular}}"
	noisily dis "\end{table}"
	
	log off
	
}
